using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using System.IO;
using Xfinity.Actors;
namespace Xfinity.Audio
{
    public static class AudioHelper
    {
        private static List<AudioEngine> allEngines = new List<AudioEngine>();
        private static Player player;
        private static AudioListener listener = new AudioListener();
        private class AudioUpdateComponent : GameComponent
        {
            public override void Update(GameTime gameTime)
            {
                UpdateListener();
                foreach (AudioEngine engine in allEngines)
                {
                    engine.Update();
                }
                base.Update(gameTime);
            }
            public AudioUpdateComponent(Game game)
                : base(game)
            {
                Enabled = true;
            }
        }
        public class AudioLibrary
        {
            public AudioLibrary(AudioEngine engine, SoundBank sb, WaveBank wb)
            {
                audioEngine = engine;
                soundBank = sb;
                waveBank = wb;
            }
            private AudioEngine audioEngine;

            /// <summary>
            /// 
            /// </summary>
            public AudioEngine AudioEngine
            {
                get { return audioEngine; }
                set { audioEngine = value; }
            }
            private SoundBank soundBank;

            /// <summary>
            /// 
            /// </summary>
            public SoundBank SoundBank
            {
                get { return soundBank; }
                set { soundBank = value; }
            }
            private WaveBank waveBank;

            /// <summary>
            /// 
            /// </summary>
            public WaveBank WaveBank
            {
                get { return waveBank; }
                set { waveBank = value; }
            }

        }
        public static AudioListener Listener
        {
            get { return listener; }
        }
        public static void InitialiseAudio(Game game, Player player)
        {
            AudioHelper.player = player;
            game.Components.Add(new AudioUpdateComponent(game));
        }
        public static AudioEngine CreateEngine(string settings)
        {
            AudioEngine engine = new AudioEngine(settings);
            allEngines.Add(engine);
            return engine;
        }
        public static AudioLibrary LoadAudioLibrary(string libraryPath)
        {
            string engineSettings = Path.ChangeExtension(libraryPath, ".xgs");
            AudioEngine engine = CreateEngine(engineSettings);
            string soundBankFile = Path.ChangeExtension(libraryPath, ".xsb");
            SoundBank soundBank = new SoundBank(engine, soundBankFile);
            string waveBankFile = Path.ChangeExtension(libraryPath, ".xwb");
            WaveBank waveBank = new WaveBank(engine, waveBankFile);
            AudioLibrary library = new AudioLibrary(engine, soundBank, waveBank);
            return library;
        }
        public static void UpdateListener()
        {
            listener.Position = player.Position;
            listener.Velocity = player.Velocity;
            listener.Forward = player.Forward;
        }
    }
}